//
//  XZHNOcJLCFMineBindCardViewControllerMRXeuQ.m
//  qcb_ios
//
//  Created by Ji on 2018/8/31.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "XZHNOcJLCFMineBindCardViewControllerMRXeuQ.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "vgxfqhJLCFMineTopUpViewControllerBVsVle.h"

@interface XZHNOcJLCFMineBindCardViewControllerMRXeuQ ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *eYZLgPnameTextFieldbQpcEE;
@property (nonatomic, strong) UITextField *NornLSIDCardTextFieldTeedFO;
@property (nonatomic, strong) UITextField *WMpYZebankCardTextFieldByTPrD;
@property (nonatomic, strong) UITextField *eHrwEGphoneTextFieldkxdmzK;;
@property (nonatomic, strong) UITextField *ZJgDiVcodeTextFieldgyblVR;
@property (nonatomic, strong) UIButton *msgBtn;

@property (nonatomic, strong) NSTimer *qdZZsdcountTimereVpqYS;
@property (nonatomic, assign) int count;

@end

static int const showtime = 60;

@implementation XZHNOcJLCFMineBindCardViewControllerMRXeuQ

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addNavigationBar];
    self.navigationBar.title = @"请填写银行卡信息";
    
    self.view.backgroundColor = QTZ_Gray237;
    
    UIButton *limitBtn = [UIButton buttonWithType:0];
    [limitBtn setTitle:@"查看银行限额" forState:0];
    [limitBtn setTitleColor:RGB(54, 124, 253) forState:0];
    limitBtn.titleLabel.font = FONT(12);
    [limitBtn sizeToFit];
    [limitBtn addTarget:self action:@selector(handleLimit) forControlEvents:UIControlEventTouchUpInside];
    limitBtn.right = FULL_WIDTH - 20;
    limitBtn.centerY = NAVBAR_CENTER_Y;
    [self.navigationBar addSubview:limitBtn];
    
    [self configView];
}

- (void)handleLimit
{
    JLCFBaseWebViewController *webVC = [[JLCFBaseWebViewController alloc] init];
    webVC.url = QTZ_WEB_URL(@"bank_list.html");
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:webVC animated:YES];
}

- (void)configView
{
    
    UIView *perBgView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT + 18, FULL_WIDTH, 90)];
    perBgView.backgroundColor = QTZ_White;
    perBgView.layer.cornerRadius = 10;
    perBgView.layer.masksToBounds = YES;
    [self.view addSubview:perBgView];

    UILabel *LufDjKnameLabelRjatWG = [UILabel labelWithText:@"真实姓名" textColor:QTZ_Gray51 font:FONT(16)];
    LufDjKnameLabelRjatWG.left = 14;
    LufDjKnameLabelRjatWG.centerY = 22.5;
    [perBgView addSubview:LufDjKnameLabelRjatWG];

    self.eYZLgPnameTextFieldbQpcEE = [[UITextField alloc] initWithFrame:CGRectMake(LufDjKnameLabelRjatWG.right + 30, 0, perBgView.width - 14 - LufDjKnameLabelRjatWG.right - 30, 30)];
    self.eYZLgPnameTextFieldbQpcEE.centerY = LufDjKnameLabelRjatWG.centerY;
    self.eYZLgPnameTextFieldbQpcEE.font = FONT(15);
    self.eYZLgPnameTextFieldbQpcEE.delegate = self;
    self.eYZLgPnameTextFieldbQpcEE.placeholder = @"请输入您的真实姓名";
    self.eYZLgPnameTextFieldbQpcEE.text = self.flFGIcbindCardmirovY ? @"" : self.card.userName;
    self.eYZLgPnameTextFieldbQpcEE.returnKeyType = UIReturnKeyDone;
    self.eYZLgPnameTextFieldbQpcEE.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.eYZLgPnameTextFieldbQpcEE.delegate = self;
    [perBgView addSubview:self.eYZLgPnameTextFieldbQpcEE];
    [LufDjKnameLabelRjatWG.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, LufDjKnameLabelRjatWG.height + 14) toPoint:CGPointMake(FULL_WIDTH - 28, LufDjKnameLabelRjatWG.height + 14) color:QTZ_Gray237 borderWidth:1]];

    UILabel *IDCardLabel = [UILabel labelWithText:@"身份证号" textColor:QTZ_Gray51 font:FONT(16)];
    IDCardLabel.left = 14;
    IDCardLabel.centerY = 22.5 + 45;
    [perBgView addSubview:IDCardLabel];

    self.NornLSIDCardTextFieldTeedFO = [[UITextField alloc] initWithFrame:CGRectMake(IDCardLabel.right + 30, 0, perBgView.width - 14 - IDCardLabel.right - 30, 30)];
    self.NornLSIDCardTextFieldTeedFO.centerY = IDCardLabel.centerY;
    self.NornLSIDCardTextFieldTeedFO.font = FONT(15);
    self.NornLSIDCardTextFieldTeedFO.delegate = self;
    self.NornLSIDCardTextFieldTeedFO.placeholder = @"请输入您的身份证号码";
    self.NornLSIDCardTextFieldTeedFO.text = self.flFGIcbindCardmirovY ? @"" : self.card.userCardNo;
    self.NornLSIDCardTextFieldTeedFO.returnKeyType = UIReturnKeyDone;
    self.NornLSIDCardTextFieldTeedFO.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.NornLSIDCardTextFieldTeedFO.delegate = self;
    [perBgView addSubview:self.NornLSIDCardTextFieldTeedFO];
    
    
    UIView *cardBgView = [[UIView alloc] initWithFrame:CGRectMake(0, perBgView.bottom + 18, FULL_WIDTH, 45)];
    cardBgView.backgroundColor = QTZ_White;
    cardBgView.layer.cornerRadius = 10;
    cardBgView.layer.masksToBounds = YES;
    [self.view addSubview:cardBgView];
    
    UILabel *bankCardLabel = [UILabel labelWithText:@"银行卡号" textColor:QTZ_Gray51 font:FONT(16)];
    bankCardLabel.left = 14;
    bankCardLabel.centerY = 22.5;
    [cardBgView addSubview:bankCardLabel];

    self.WMpYZebankCardTextFieldByTPrD = [[UITextField alloc] initWithFrame:CGRectMake(bankCardLabel.right + 30, 0, cardBgView.width - 14 - bankCardLabel.right - 30, 30)];
    self.WMpYZebankCardTextFieldByTPrD.centerY = bankCardLabel.centerY;
    self.WMpYZebankCardTextFieldByTPrD.font = FONT(15);
    self.WMpYZebankCardTextFieldByTPrD.delegate = self;
    self.WMpYZebankCardTextFieldByTPrD.placeholder = @"请输入您的银行卡号";
    self.WMpYZebankCardTextFieldByTPrD.text = self.flFGIcbindCardmirovY ? @"" : self.card.bankCardNo;
    self.WMpYZebankCardTextFieldByTPrD.returnKeyType = UIReturnKeyDone;
    self.WMpYZebankCardTextFieldByTPrD.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.WMpYZebankCardTextFieldByTPrD.delegate = self;
    [cardBgView addSubview:self.WMpYZebankCardTextFieldByTPrD];
    [bankCardLabel.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, bankCardLabel.height + 14) toPoint:CGPointMake(FULL_WIDTH - 28, bankCardLabel.height + 14) color:QTZ_Gray237 borderWidth:1]];
    
//    UILabel *jyvhKwphoneLabelpopXEs = [UILabel labelWithText:@"预留手机号" textColor:QTZ_Gray51 font:FONT(16)];
//    jyvhKwphoneLabelpopXEs.left = 14;
//    jyvhKwphoneLabelpopXEs.centerY = 22.5 + 45;
//    [cardBgView addSubview:jyvhKwphoneLabelpopXEs];

//    self.eHrwEGphoneTextFieldkxdmzK = [[UITextField alloc] initWithFrame:CGRectMake(self.WMpYZebankCardTextFieldByTPrD.left, 0, cardBgView.width - 14 - jyvhKwphoneLabelpopXEs.right - 30, 30)];
//    self.eHrwEGphoneTextFieldkxdmzK.centerY = jyvhKwphoneLabelpopXEs.centerY;
//    self.eHrwEGphoneTextFieldkxdmzK.font = FONT(15);
//    self.eHrwEGphoneTextFieldkxdmzK.delegate = self;
//    self.eHrwEGphoneTextFieldkxdmzK.placeholder = @"请输入您的手机号";
//    self.eHrwEGphoneTextFieldkxdmzK.returnKeyType = UIReturnKeyDone;
//    self.eHrwEGphoneTextFieldkxdmzK.clearButtonMode = UITextFieldViewModeWhileEditing;
//    self.eHrwEGphoneTextFieldkxdmzK.delegate = self;
//    [cardBgView addSubview:self.eHrwEGphoneTextFieldkxdmzK];
//    [jyvhKwphoneLabelpopXEs.layer addSublayer:[self addLineOriginPoint:CGPointMake(0, jyvhKwphoneLabelpopXEs.height + 14) toPoint:CGPointMake(FULL_WIDTH - 28, jyvhKwphoneLabelpopXEs.height + 14) color:QTZ_Gray237 borderWidth:1]];

//    self.msgBtn = [UIButton new];
//    self.msgBtn.titleLabel.font = FONT(12);
//    [self.msgBtn setTitleColor:Theme_Color forState:0];
//    [self.msgBtn setTitle:@"获取验证码" forState:0];
//    [self.msgBtn sizeToFit];
//    [self.msgBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
//    self.eHrwEGphoneTextFieldkxdmzK.rightView = self.msgBtn;
//    self.eHrwEGphoneTextFieldkxdmzK.rightViewMode = UITextFieldViewModeAlways;
//
//    UILabel *codeLabel = [UILabel labelWithText:@"充值金额" textColor:QTZ_Gray51 font:FONT(16)];
//    codeLabel.left = 14;
//    codeLabel.centerY = 22.5 + 90;
//    [cardBgView addSubview:codeLabel];
    
//    self.ZJgDiVcodeTextFieldgyblVR = [[UITextField alloc] initWithFrame:CGRectMake(self.WMpYZebankCardTextFieldByTPrD.left, 0, cardBgView.width - 14 - codeLabel.right - 30, 30)];
//    self.ZJgDiVcodeTextFieldgyblVR.centerY = codeLabel.centerY;
//    self.ZJgDiVcodeTextFieldgyblVR.font = FONT(15);
//    self.ZJgDiVcodeTextFieldgyblVR.delegate = self;
//    self.ZJgDiVcodeTextFieldgyblVR.placeholder = @"请输入您的验证码";
//    self.ZJgDiVcodeTextFieldgyblVR.returnKeyType = UIReturnKeyDone;
//    self.ZJgDiVcodeTextFieldgyblVR.clearButtonMode = UITextFieldViewModeWhileEditing;
//    self.ZJgDiVcodeTextFieldgyblVR.delegate = self;
//    [cardBgView addSubview:self.ZJgDiVcodeTextFieldgyblVR];
    
    UIButton *bindBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, 0, FULL_WIDTH - 76, 40)];
    bindBtn.bottom = FULL_HEIGHT - 15 - (IPHONEX ? 30 : 0);
    bindBtn.titleLabel.font = FONT(15);
    [bindBtn setBackgroundColor:Theme_Color];
    [bindBtn setTitleColor:QTZ_White forState:0];
    [bindBtn setTitle:@"下一步"forState:0];
    bindBtn.layer.cornerRadius = 5;
    [bindBtn addTarget:self action:@selector(handleBind) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bindBtn];
    
}

- (void)handleBind
{
    
    
    //首先判定卡片是否支持富有支付
    [sHkPtEJLCFMineRequestozAYZs isBanckCardSuportWithparams:@{@"bankCardNo":self.WMpYZebankCardTextFieldByTPrD.text} loadingInView:self.view success:^(FSgGvPJLCFCardInfoModelLPToqL *data){
        
        //执行完成block
        if (self.flFGIcbindCardmirovY) {
            MineTopUpViewController *vc = [MineTopUpViewController new];
//            vc.data = self.car;
            vc.JdRIdxcardinfoDataQaLyXt = data;
            vc.name = self.eYZLgPnameTextFieldbQpcEE.text;
            vc.pid = self.NornLSIDCardTextFieldTeedFO.text;;
            [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController pushViewController:vc animated:YES];
        }
//        self.bindComplete();
        
//        NSMutableDictionary *params = [NSMutableDictionary dictionary];
//        [params setSafeObject:self.eYZLgPnameTextFieldbQpcEE.text forKey:@"name"];
//        [params setSafeObject:self.WMpYZebankCardTextFieldByTPrD.text forKey:@"bankCard"];
//        [params setSafeObject:self.NornLSIDCardTextFieldTeedFO.text forKey:@"idCard"];
//        [params setSafeObject:self.eHrwEGphoneTextFieldkxdmzK.text forKey:@"phone"];
//        [params setSafeObject:self.ZJgDiVcodeTextFieldgyblVR.text forKey:@"msgCode"];
//
//        [sHkPtEJLCFMineRequestozAYZs flFGIcbindCardmirovYWithParams:params loadingInView:self.view success:^{
////            [self showNotice:@"绑卡成功"];
//            if (self.bindComplete) {
//                self.bindComplete();
//            }
//            [[[JLCFApplicationEntrace shareEntrance] currentNavController] popViewControllerAnimated:YES];
//
//        } failure:^(NSString *error) {
//            [self showNotice:error];
//        }];
        
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
    
}

//获取验证码
- (void)getVerifyCode
{
    if (self.flFGIcbindCardmirovY) {//绑卡
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setSafeObject:self.eYZLgPnameTextFieldbQpcEE.text forKey:@"name"];
        [params setSafeObject:self.WMpYZebankCardTextFieldByTPrD.text forKey:@"bankCard"];
        [params setSafeObject:self.NornLSIDCardTextFieldTeedFO.text forKey:@"idCard"];
        [params setSafeObject:self.eHrwEGphoneTextFieldkxdmzK.text forKey:@"phone"];
        
        [sHkPtEJLCFMineRequestozAYZs flFGIcbindCardmirovYSendMsgWithParams:params loadingInView:self.view success:^{
            [self handleVerifyBtn];
            [self showNotice:@"验证码发送成功"];
        } failure:^(NSString *error) {
            [self showNotice:error];
        }];
        
    } else {//解绑
//        NSMutableDictionary *params = [NSMutableDictionary dictionary];
//        [params setSafeObject:self.eHrwEGphoneTextFieldkxdmzK.text forKey:@"phone"];
        
    }
}

- (void)handleVerifyBtn
{
    _count = showtime;
    [[NSRunLoop mainRunLoop] addTimer:self.qdZZsdcountTimereVpqYS forMode:NSRunLoopCommonModes];
}

- (NSTimer *)qdZZsdcountTimereVpqYS
{
    if (!_qdZZsdcountTimereVpqYS) {
        _qdZZsdcountTimereVpqYS = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _qdZZsdcountTimereVpqYS;
}

- (void)countDown
{
    _count --;
    [self.msgBtn setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:QTZ_Gray173 forState:0];
    self.msgBtn.userInteractionEnabled = NO;
    if (_count == 0) {
        [self.msgBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:Theme_Color forState:0];
        self.msgBtn.userInteractionEnabled = YES;
        [self dismiss];
    }
    [self.msgBtn sizeToFit];
}

- (void)dismiss
{
    [self.qdZZsdcountTimereVpqYS invalidate];
    self.qdZZsdcountTimereVpqYS = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)dZrvUEtextFieldRRHEAtShouldReturn:(UITextField *)dZrvUEtextFieldRRHEAt{
    return [dZrvUEtextFieldRRHEAt resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)dZrvUEtextFieldRRHEAt:(UITextField *)dZrvUEtextFieldRRHEAt shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (dZrvUEtextFieldRRHEAt == self.eHrwEGphoneTextFieldkxdmzK) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.eHrwEGphoneTextFieldkxdmzK.text.length >= 11) {
            self.eHrwEGphoneTextFieldkxdmzK.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:11];
            return NO;
        }
    }
    
    if (dZrvUEtextFieldRRHEAt == self.ZJgDiVcodeTextFieldgyblVR) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.ZJgDiVcodeTextFieldgyblVR.text.length >= 6) {
            self.ZJgDiVcodeTextFieldgyblVR.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:6];
            return NO;
        }
    }
    
    return YES;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth
{
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * ZLmcEJshapeLayerWDClkp = [CAShapeLayer layer];
    ZLmcEJshapeLayerWDClkp.strokeColor = color.CGColor;
    ZLmcEJshapeLayerWDClkp.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    ZLmcEJshapeLayerWDClkp.path = bezierPath.CGPath;
    /// 线宽度
    ZLmcEJshapeLayerWDClkp.lineWidth = borderWidth;
    return ZLmcEJshapeLayerWDClkp;
}

@end
